package com.blb.marrysystem.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.marrysystem.dao.TbHotelDao;
import com.blb.marrysystem.entity.*;
import com.blb.marrysystem.entity.dto.DetailsPackageDto;
import com.blb.marrysystem.entity.dto.DetailsRoomDto;
import com.blb.marrysystem.entity.dto.HotelDto;
import com.blb.marrysystem.entity.dto.HotelPackageDto;
import com.blb.marrysystem.service.TbHotelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TbHotel)表服务实现类
 *
 * @author makejava
 * @since 2023-01-11 09:22:30
 */
@Service
public class TbHotelServiceImpl extends ServiceImpl<TbHotelDao,TbShoppingcart> implements TbHotelService {
    @Resource
    private TbHotelDao tbHotelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param hotelId 主键
     * @return 实例对象
     */
    @Override
    public TbHotel queryById(Integer hotelId) {
        return this.tbHotelDao.queryById(hotelId);
    }

    /**
     * 分页查询
     *

     * @return 查询结果
     */
    @Override
    public Page<HotelDto> queryByPage(Integer begin,String latest,String search) {
        if("".equals(search)||search==null){
            Page<HotelDto> hotelDtoPage = tbHotelDao.queryByPage(new Page<HotelDto>(begin, 8), latest);
            return hotelDtoPage;
        }else {
            Page<HotelDto> hotelDtoPage=tbHotelDao.queryByPageSearch(new Page<HotelDto>(begin,8),latest,"%"+search+"%");
            return hotelDtoPage;
        }
    }

    /**
     * 新增数据
     *
     * @param tbHotel 实例对象
     * @return 实例对象
     */
    @Override
    public TbHotel insert(TbHotel tbHotel) {
        this.tbHotelDao.insert(tbHotel);
        return tbHotel;
    }

    /**
     * 修改数据
     *
     * @param tbHotel 实例对象
     * @return 实例对象
     */
    @Override
    public TbHotel update(TbHotel tbHotel) {
        this.tbHotelDao.update(tbHotel);
        return this.queryById(tbHotel.getHotelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param hotelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer hotelId) {
        return this.tbHotelDao.deleteById(hotelId) > 0;
    }

    @Override
    public List<DetailsRoomDto> detailsRoomList(HotelDto hotelDto) {
        List<DetailsRoomDto> detailsRoomDtoList = tbHotelDao.detailsRoomList(hotelDto);
        System.out.println(detailsRoomDtoList);
        return detailsRoomDtoList;
    }

    @Override
    public List<TbInformation> detailsInformation(HotelDto hotelDto) {
        return tbHotelDao.detailsInformation(hotelDto);
    }

    @Override
    public List<DetailsPackageDto> detailsPackage(HotelDto hotelDto) {
        return tbHotelDao.detailsPackage(hotelDto);
    }

    @Override
    public TbPackage packageInformation(String packageId) {
        List<TbPackage> tbPackageList=tbHotelDao.packageInformation(packageId);
        return tbPackageList.get(0);
    }

    @Override
    public List<HotelDto> hotelList() {
        List<HotelDto> hotelDtoList = tbHotelDao.hotelList();
        return hotelDtoList;
    }

    @Override
    public List<DetailsPackageDto> otherPackageInformation(String packageId, Integer tbHotelHotelId) {
        List<DetailsPackageDto> detailsPackageDtoList=tbHotelDao.otherPackageInformation(packageId,tbHotelHotelId);
        return detailsPackageDtoList;
    }

    @Override
    public List<HotelPackageDto> selectHotelList() {

        return tbHotelDao.selectHotelList();
    }


    @Override
    public TbUser login(String telephoneNumber, String password) throws Exception {
        List<TbUser> tbUsers=tbHotelDao.loginText(telephoneNumber);
        if(tbUsers.size()!=0){
            if(password.equals(tbUsers.get(0).getUserPassword())){
                return tbUsers.get(0);
            }else {
                throw new Exception("密码错误");
            }
        }else {
            throw new Exception("账户不存在");
        }

    }

    @Override
    public void register(String telephoneNumber, String passWord) throws Exception {
        List<TbUser> tbUsers=tbHotelDao.loginText(telephoneNumber);
        if(tbUsers.size()==0){
            tbHotelDao.userInsert(telephoneNumber,passWord);
        }else {
            throw new Exception("账户已存在");
        }
    }

    @Override
    public List<TbShoppingcart> shopCart(String userTel) {

        return tbHotelDao.shopCart(userTel);
    }

    @Override
    public void shopPingCartInsert(TbShoppingcart tbShoppingcart) {
        tbHotelDao.insert(tbShoppingcart);
    }

    @Override
    public void computer(String number, String shopId) {
        tbHotelDao.computerUpdate(number,shopId);
    }

    @Override
    public List<TbOrder> order(String userTel) {
        return tbHotelDao.order(userTel);
    }

    @Override
    public void orderInsert(TbOrder tbOrder) {
        tbHotelDao.orderInsert(tbOrder);
    }

}
